using UnityEngine;
using UnityEngine.InputSystem;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System;

namespace GGameFramework.MInput
{
    
}

[CreateAssetMenu(fileName = "InputConfig", menuName = "Input System/Input Configuration")]
public class InputConfigManager : ScriptableObject
{
    [Header("输入动作资源")]
    public InputActionAsset inputActions;

    [Header("默认绑定配置")]
    public string defaultBindings;

    [Header("组合键配置")]
    public List<CompositeBinding> compositeBindings = new List<CompositeBinding>();

    [System.Serializable]
    public class CompositeBinding
    {
        public string actionName;
        public List<string> requiredKeys = new List<string>();
        [NonSerialized] public bool isPressed;
    }

    private void OnEnable()
    {
        // 加载保存的配置
        LoadAllBindings();
    }
    
    // 加载所有绑定配置
    public void LoadAllBindings()
    {
        LoadBindings();
        LoadCompositeBindings();
    }
    // 初始化输入系统
    public void Initialize()
    {
        if (inputActions == null) 
        {
            Debug.LogError("InputConfigManager: InputActions asset is not assigned!");
            return;
        }
        
        // 确保输入动作已启用
        inputActions.Enable();
        
        // 加载默认绑定
        if (!string.IsNullOrEmpty(defaultBindings))
        {
            inputActions.LoadBindingOverridesFromJson(defaultBindings);
        }
        
        // 应用保存的绑定
        ApplySavedBindings();
    }

    // 获取输入动作
    public InputAction GetAction(string actionName)
    {
        return inputActions?.FindAction(actionName);
    }

    // 保存当前绑定配置
    public void SaveBindings()
    {
        if (inputActions == null) return;
        
        string bindingsJson = inputActions.SaveBindingOverridesAsJson();
        PlayerPrefs.SetString("InputBindings", bindingsJson);
        PlayerPrefs.Save();
    }

    // 应用保存的绑定
    private void ApplySavedBindings()
    {
        if (PlayerPrefs.HasKey("InputBindings"))
        {
            string savedBindings = PlayerPrefs.GetString("InputBindings");
            inputActions.LoadBindingOverridesFromJson(savedBindings);
        }
    }

    // 加载绑定配置
    private void LoadBindings()
    {
        if (PlayerPrefs.HasKey("InputBindings"))
        {
            string savedBindings = PlayerPrefs.GetString("InputBindings");
            if (!string.IsNullOrEmpty(savedBindings) && inputActions != null)
            {
                inputActions.LoadBindingOverridesFromJson(savedBindings);
            }
        }
    }
    
    // 重置为默认绑定
    public void ResetToDefaultBindings()
    {
        if (!string.IsNullOrEmpty(defaultBindings))
        {
            inputActions.LoadBindingOverridesFromJson(defaultBindings);
            SaveBindings();
        }
    }

    // 保存组合键配置
    public void SaveCompositeBindings()
    {
        CompositeBindingsData data = new CompositeBindingsData
        {
            compositeBindings = compositeBindings
        };
        
        string json = JsonUtility.ToJson(data);
        PlayerPrefs.SetString("CompositeBindings", json);
        PlayerPrefs.Save();
    }

    // 加载组合键配置
    private void LoadCompositeBindings()
    {
        if (PlayerPrefs.HasKey("CompositeBindings"))
        {
            string json = PlayerPrefs.GetString("CompositeBindings");
            if (!string.IsNullOrEmpty(json))
            {
                CompositeBindingsData data = JsonUtility.FromJson<CompositeBindingsData>(json);
                if (data != null)
                {
                    compositeBindings = data.compositeBindings;
                }
            }
        }
    }


    
    // 获取组合键配置
    public CompositeBinding GetCompositeBinding(string actionName)
    {
        return compositeBindings.FirstOrDefault(cb => cb.actionName == actionName);
    }

    // 添加或更新组合键配置
    public void SetCompositeBinding(string actionName, List<string> keys)
    {
        var existing = compositeBindings.FirstOrDefault(cb => cb.actionName == actionName);
        
        if (existing != null)
        {
            existing.requiredKeys = keys;
        }
        else
        {
            compositeBindings.Add(new CompositeBinding
            {
                actionName = actionName,
                requiredKeys = keys
            });
        }
        
        SaveCompositeBindings();
    }

    // 清除指定动作的组合键配置
    public void ClearCompositeBinding(string actionName)
    {
        compositeBindings.RemoveAll(cb => cb.actionName == actionName);
        SaveCompositeBindings();
    }
    
    [System.Serializable]
    private class CompositeBindingsData
    {
        public List<CompositeBinding> compositeBindings;
    }
}
